package mt.dummy;

import static org.junit.Assert.*;

import javax.swing.JTextField;

import mt.Order;
import mt.client.fake.ClientCommFake;
import mt.client.fake.MessageFromServerFake;
import mt.comm.ClientComm;
import mt.comm.ClientSideMessage.Type;

import org.junit.Before;
import org.junit.Test;

public class MicroTraderClientDummyTest {
	
	private ClientComm clientComm;
	private ClientCommFake clientCommFake;
	private MicroTraderClientDummy microTraderClient;
	private MessageFromServerFake messageFromServerFake;
	
	private static String nickName = "nickname";
	private static String stock = "stock";
	private static int numberOfUnits = 10;
	private static double pricePerUnit = 10;
	
	@Before
	public void init() {
		microTraderClient = new MicroTraderClientDummy();
		Order order = Order.createBuyOrder(nickName, stock, numberOfUnits, pricePerUnit);
		messageFromServerFake = new MessageFromServerFake(Type.ORDER, order);
	}
	
	@Test
	public void testStart() {
		clientComm = new ClientCommDummy();
		microTraderClient.start(clientComm);
		
		clientCommFake = new ClientCommFake();
		clientCommFake.addMessage(messageFromServerFake);
		microTraderClient.start(clientCommFake);
		microTraderClient.setNickName(new JTextField("NickName"));
		microTraderClient.getLoginButton().doClick();
		
		Order order = Order.createBuyOrder(nickName, stock, numberOfUnits, pricePerUnit);
		messageFromServerFake = new MessageFromServerFake(Type.ORDER, order);
		
		clientCommFake = new ClientCommFake();
		clientCommFake.addMessage(messageFromServerFake);
		microTraderClient.start(clientCommFake);
		microTraderClient.setNickName(new JTextField(""));
		microTraderClient.getLoginButton().doClick();
		
		order = Order.createBuyOrder(nickName, stock, numberOfUnits, pricePerUnit);
		messageFromServerFake = new MessageFromServerFake(Type.ORDER, order);
		
		clientCommFake = new ClientCommFake();
		clientCommFake.addMessage(messageFromServerFake);
		microTraderClient.start(clientCommFake);
		microTraderClient.setNickName(new JTextField("NickName"));
		clientCommFake.disconnect();
		microTraderClient.getLoginButton().doClick();
		
		messageFromServerFake = new MessageFromServerFake(Type.ERROR);
		
		clientCommFake = new ClientCommFake();
		clientCommFake.addMessage(messageFromServerFake);
		microTraderClient.start(clientCommFake);
		microTraderClient.setNickName(new JTextField("NickName"));
		microTraderClient.getLoginButton().doClick();
	}

}
